package com.bonc.rms.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.bonc.rms.entity.SeniorQueryEntity;
import com.bonc.rms.entity.TestEntity;
import com.bonc.rms.service.QueryService;

/**
 * 搜索控制器
 * 
 * @author FlyingKiller
 */
@Controller
public class QueryController {
	@Autowired
	QueryService queryService;

	/**
	 * 首页搜索
	 */
	@RequestMapping(value = "/select/detailQuery")
	public ModelAndView detailQueryResume(@RequestParam(value = "queryValue", required = false) String queryValue,
			@RequestParam(value = "show_select", required = false) String show_select,
			@RequestParam(value = "pageNo", defaultValue = "0") int pageNo, HttpSession httpSession) {
		ModelAndView modelAndView = new ModelAndView("select");
		List<Map<String, Object>> detailQuerytList = queryService.detailQueryResume(queryValue, pageNo);

		modelAndView.addObject("pageDataMap", detailQuerytList.get(detailQuerytList.size() - 1));
		detailQuerytList.remove(detailQuerytList.size() - 1);
		modelAndView.addObject("resultList", detailQuerytList);
		modelAndView.addObject("queryValue", queryValue);
		modelAndView.addObject("show_select", show_select);
		// 将搜索显示列存入mysql,参数为对应实体，注意:现方法参数为举例而非最终版本
		// queryService.saveShowSelect(new TestEntity());
		// httpSession.setAttribute("show_select", show_select);
		return modelAndView;
	}

	/**
	 * 高级搜索
	 */
	@RequestMapping(value = "/detailSelect/seniorQuery")
	public ModelAndView seniorQuery(@RequestParam(value = "pageNo", defaultValue = "0") int pageNo,
			SeniorQueryEntity seniorQueryEntity, HttpSession httpSession) {
		if (seniorQueryEntity.getTag() == true) {
			seniorQueryEntity = (SeniorQueryEntity) httpSession.getAttribute("sessionQueryEntity");
		}
		httpSession.setAttribute("sessionQueryEntity", seniorQueryEntity);
		List<Map<String, Object>> seniorQuerytList = queryService.seniorQueryResume(seniorQueryEntity, pageNo);
		ModelAndView modelAndView = new ModelAndView("detailSelect");
		modelAndView.addObject("pageDataMap", seniorQuerytList.get(seniorQuerytList.size() - 1));
		seniorQuerytList.remove(seniorQuerytList.size() - 1);
		modelAndView.addObject("resultList", seniorQuerytList);
		return modelAndView;

	}
}
